import 'package:connectivity/connectivity.dart';
import 'package:dio/dio.dart';
import 'package:flutter_module/beans/Entity.dart';
import 'package:flutter_module/beans/ShareBean.dart';
import 'package:flutter_module/beans/appkey.dart';
import 'package:flutter_module/beans/conmmonResult.dart';
import 'package:flutter_module/commom/user.dart';
import 'package:flutter_module/config/api.dart';
import 'package:flutter_module/http/dio_manager.dart';
import 'package:flutter_module/http/UserModel.dart';
import 'package:oktoast/oktoast.dart';


class ApiService {

  Future<bool> isConnected() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    return connectivityResult != ConnectivityResult.none;
  }
  checkConnect() async{
    var connectivityResult = await (Connectivity().checkConnectivity());
    if(connectivityResult == ConnectivityResult.none){
      showToast("请检查网络");
      return;
    }
  }
  /// 登录
  void login(Function callback, String _username, String _password) async {
    await checkConnect();
    FormData formData =
        new FormData.fromMap({"username": _username, "password": _password});
    DioManager.singleton.dio
        .post(API.USER_LOGIN, data: {"username": _username, "password": _password}, options: _getOptions())
        .then((response) {
      callback(UserModel.fromJson(response.data), response);
    });
  }

    /// 验证吗
  void sendPhoneSMS(Function callback, String mobile, String smsmode) async {
    await checkConnect();
    print({"mobile": mobile, "smsmode": smsmode});
    DioManager.singleton.dio
        .post(API.SENDSMS, data: {"mobile": mobile, "smsmode": smsmode}, options: _getOptions())
        .then((response) {
          print(response);
      callback(response);
    });
  }

  /// 获取ysy新密钥
  void getYsyToekn(Function callback) async {

    DioManager.singleton.dio
        .get(API.GET_YSYTOEN, options: null)
        .then((response) {
      print(response.toString());
      callback(Appkey.fromJson(response.data));
    });
  }

  /// 注册
  void register(Function callback, String _username, String _password, String mobile, String smsmode) async {
    await checkConnect();
    FormData formData = new FormData.fromMap({
      "username": _username,
      "password": _password,
      "uuid": _password
    });
    print(_username+"  "+_password+"  "+mobile+"  "+smsmode);
    DioManager.singleton.dio
        .post(API.USER_REGISTER, data: {"username": _username, "password": _password,
        "phone": mobile,"smscode": smsmode}, options: null)
        .then((response) {
      print(response.toString());
      callback(UserModel.fromJson(response.data));
    });
  }
  /// 修改密码
  void repassword(Function callback, String phone, String password, String smsmode) async {
    DioManager.singleton.dio
        .post(API.USER_REPASSWORD, data: {"phone": phone, "password": password, "smscode": smsmode}, options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }

  /// 轮询接口是否绑定成功
  void appPair(Function callback,String lanip, String uuid,String deviceType) async {
    // +uuid+"&deviceType="+typekey+"&lanip="+ip
    DioManager.singleton.dio
        .get(API.APP_PAIR+uuid+"&deviceType="+deviceType+"&lanip="+lanip, options: null)
        .then((response) {
      print(response.toString());

      callback(response);
    });
  }

  /// 删除分享
  void deleteShare(Function callback, String id) async {
    DioManager.singleton.dio
        .get(API.deleteShare+id, options: null)
        .then((response) {
      callback(response);
    });
  }

  /// 删除设备
  void deleteBychipid(Function callback, String chipid) async {
    FormData formData = new FormData.fromMap({
      "chipid": chipid
    });
    DioManager.singleton.dio
        .post(API.deleteDevice, data: {"chipid": chipid}, options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }

  /// 分享设备
  void ShareESP(Function callback, ShareBean shareBean,bool isUpdateShare) async {
    var url;
    if(isUpdateShare){
      url = API.updateShare;
    }else{
      url = API.SHARE;
    }
    DioManager.singleton.dio
        .post(url, data: shareBean.toJson(), options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }

  /// 修改设备
  void ESPEDIT(Function callback, App_esp_ip model) async {
//    FormData formData = new FormData.from({
//      "chipid": chipid
//    });
    DioManager.singleton.dio
        .post(API.ESPEDIT, data: model.toJson(), options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }
  /// getDeviceTypeList
  void getDeviceTypeList(Function callback) async {
    // +uuid+"&deviceType="+typekey+"&lanip="+ip
    DioManager.singleton.dio
        .get(API.DeviceTypeList, options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }

  void getUUID(Function callback) async {
    DioManager.singleton.dio
        .get(API.GET_UUID)
        .then((response) {
      print(response.toString());
      callback(response.message);
    });
  }


  Options _getOptions() {
    Map<String, String> map = new Map();
    List<String> cookies = User().cookie;
    map["Cookie"] = cookies.toString();
    return Options(headers: map);
  }

  void getNewToken(Function callback) async {
    // +uuid+"&deviceType="+typekey+"&lanip="+ip
    DioManager.singleton.dio
        .get(API.GetNewAuth, options: null)
        .then((response) {
      print(response.toString());
      callback(response);
    });
  }

  void getNewTokenByAppkey(String secret, String xlh,Function callback) async {
    DioManager.singleton.dio
        .get(API.GetNewAuthForPlay+'?secret='+secret+'&xlh='+xlh, options: null)
        .then((response) {
      print(response.toString());
      ConmmonResult conmmonResult = ConmmonResult.fromJson(response.data);
      callback(conmmonResult);
    });
  }

}
